package com.freemodbus4j.network.channel;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;


public abstract class ModbusChannelInitializer extends ChannelInitializer<SocketChannel> {

    private final SimpleChannelInboundHandler handler;

    protected ModbusChannelInitializer(SimpleChannelInboundHandler handler) {
        this.handler = handler;
    }

    //默认内置的的handler，和扩展的handler
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        ChannelPipeline pipeline = socketChannel.pipeline();

        pipeline.addLast("loggerHandler",new LoggingHandler(LogLevel.INFO));

        this.initPipelinePreProcessHandler(pipeline);

        if (this.handler != null) {
            pipeline.addLast("responseHandler",handler);
        }

        this.initPipelinePostProcessHandler(pipeline);
    }


    protected abstract void initPipelinePreProcessHandler(ChannelPipeline pipeline) ;



    protected abstract void initPipelinePostProcessHandler(ChannelPipeline pipeline) ;
}
